package net.tngou.iask.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.jdbc.QueryHelper;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.util.StringUtil;

/**
 * 医学统计
 * @author tngou@tngou.net
 *
 */
public class MedicalDao extends BaseDao {
	
	/**
	 * 
	 * @param page
	 * @param size
	 * @param main
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main) {
		List<Map<String, Object>> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT medical.*,disease.name ";
		String sql_t="SELECT COUNT(*) ";
		String sql=" FROM `iask_statisticmedical` medical,`iask_disease` disease WHERE"
				+ "  medical.disease=disease.id AND medical.main=? ";			
		String sql_o=" ORDER BY medical.id DESC";//排序		
		Object[] params={main};
		list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);			
		total=(int) QueryHelper.stat(sql_t+sql, params);
		
		return new PageUtil(list, page, size, total);
	}
	
	
	/**
	 * 患者数
	 * @param main
	 * @param disease
	 * @return
	 */
	public int patientcount(long main,long disease) {
		String sql="SELECT COUNT(DISTINCT patient) FROM `iask_cure` WHERE main=? AND disease=?";
		return (int) QueryHelper.stat(sql, main,disease);
	}
	
	
	/**
	 * 取得治疗数
	 * @param main
	 * @param disease
	 * @return
	 */
	public int curecount(long main,long disease) {
		String sql="SELECT COUNT(*) FROM `iask_cure` WHERE main=? AND disease=?";
		return (int) QueryHelper.stat(sql, main,disease);
	}
	
	public int curecount(long main,long disease,int type) {
		String sql="SELECT COUNT(*) FROM `iask_cure` WHERE main=? AND disease=? AND type=?";
		return (int) QueryHelper.stat(sql, main,disease,type);
	}
	
	/**
	 * 随访次数
	 * @param main
	 * @param disease
	 * @return
	 */
	public int accesscount(long main,long disease) {
		String sql="SELECT COUNT(access.id) FROM  `iask_access` access,`iask_cure` cure WHERE access.cure=cure.id AND  cure.main=? AND cure.disease=?";
		return (int) QueryHelper.stat(sql, main,disease);
	}
	
	public int accesscount(long main,long disease,int state) {
		String sql="SELECT COUNT(access.id) FROM  `iask_access` access,`iask_cure` cure WHERE access.cure=cure.id AND access.state=? AND cure.main=? AND cure.disease=?";
		return (int) QueryHelper.stat(sql,state, main,disease);
	}
	/**
	 * 有效
	 * @param main
	 * @param disease
	 * @return
	 */
	public int effective(long main,long disease) {
		String sql=" SELECT COUNT(*) FROM (SELECT patient,type, COUNT(patient) as ct FROM `iask_cure` WHERE main=? AND disease=? GROUP BY patient) cure WHERE cure.ct=1 AND cure.type=1";
		return (int) QueryHelper.stat(sql, main,disease);
	}

	/**
	 * 死亡数
	 * @param main
	 * @param disease
	 * @return
	 */
	public int mortality(long main,long disease) {
		String sql="SELECT COUNT(obituary.id) FROM  `iask_obituary` obituary,`iask_cure` cure WHERE obituary.patient=cure.patient AND  cure.main=? AND cure.disease=?";
		return (int) QueryHelper.stat(sql, main,disease);
	}
	
	/**
	 *  最近N年患者总数
	 * @param main
	 * @param disease
	 * @param year
	 * @return
	 */
	public int generationtotal(long main,long disease,int year) {
		String sql="SELECT COUNT(DISTINCT patient) FROM `iask_cure` WHERE main=? AND disease=? AND DATE_ADD(NOW(),INTERVAL ? YEAR)>tdate";	
		return (int) QueryHelper.stat(sql, main,disease,year);
		
	}
	
	/**
	 * 最近N年患者死亡
	 * @param main
	 * @param disease
	 * @param year
	 * @return
	 */
	public int generation(long main,long disease,int year) {
		String sql="SELECT COUNT(*) FROM `iask_obituary` WHERE patient IN( SELECT DISTINCT patient FROM `iask_cure` WHERE main=? AND disease=? AND DATE_ADD(NOW(),INTERVAL ? YEAR)<tdate)";
		return (int) QueryHelper.stat(sql, main,disease,year);
		
	}
	
}
